<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let arr = [
            { id: 1, name: '部门1', pid: 0 },
            { id: 2, name: '部门2', pid: 1 },
            { id: 3, name: '部门3', pid: 1 },
            { id: 4, name: '部门4', pid: 3 },
            { id: 5, name: '部门5', pid: 4 },
        ]
        const mu = [
            {
                "id": 1,
                "name": "部门1",
                "pid": 0,
                "children": [
                    {
                        "id": 2,
                        "name": "部门2",
                        "pid": 1,
                        "children": []
                    },
                    {
                        "id": 3,
                        "name": "部门3",
                        "pid": 1,
                        "children": [
                            // 结果 ,,,
                        ]
                    }
                ]
            }
        ]

        //-1-数据树形化--性能最优-----------

        function arrayToTree(items) {
            const result = [];   // 存放结果集
            const itemMap = {};  // 
            for (const item of items) {
                //item = { id: 1, name: '部门1', pid: 0 },
                const id = item.id;
                const pid = item.pid;

                if (!itemMap[id]) {
                    itemMap[id] = {
                        children: [],
                    }
                    
                }
                
                //itemMap={ id: { children:[] } }

                itemMap[id] = {
                    ...item,
                    children: itemMap[id]['children']// itemMap[id]['children']=[]
                }
                // itemMap={"1": {"id":1,"name":"部门1","pid":0,"children":[]}}

                const treeItem = itemMap[id];
                //reeItem = { id: 1, name: '部门1', pid: 0, children: [] };

                if (pid === 0) {
                    result.push(treeItem);//
                } else {
                    //此pid不存在时，添加一个并将children初始化为[]
                    if (!itemMap[pid]) {
                        itemMap[pid] = {
                            children: [],
                        }
                    }
                    //已经存在直接push进去
                    itemMap[pid].children.push(treeItem)
                }

            }
            return result;
        }
        let ad = arrayToTree(arr);
        console.log(ad);

        //---2-不用递归----------------

        function arrayToTree_two(items) {
            const result = [];   // 存放结果集
            const itemMap = {};  // 

            // 先转成map存储
            for (const item of items) {
                itemMap[item.id] = { ...item, children: [] }
            }

            for (const item of items) {
                const id = item.id;
                const pid = item.pid;
                const treeItem = itemMap[id];
                if (pid === 0) {
                    result.push(treeItem);
                } else {
                    if (!itemMap[pid]) {
                        itemMap[pid] = {
                            children: [],
                        }
                    }
                    itemMap[pid].children.push(treeItem)
                }

            }
            return result;
        }


        // ----3-使用递归------------------

        /**
         * 递归查找，获取children
         */
        const getChildren = (data, result, pid) => {
            for (const item of data) {
                if (item.pid === pid) {
                    const newItem = { ...item, children: [] };
                    result.push(newItem);
                    getChildren(data, newItem.children, item.id);
                }
            }
        }

        /**
        * 转换方法
        */
        const arrayToTree_last = (data, pid) => {
            const result = [];
            getChildren(data, result, pid)
            return result;
        }

    </script>
</body>

</html>